﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Threading;

namespace Addins.UI.ViewModel
{
    public abstract class BaseViewModel : MarshalByRefObject, INotifyPropertyChanged
    {
        #region Events (Public)

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Instance Fields (Protected)

        /// <summary>
        /// The context in which the object was created
        /// </summary>
        protected SynchronizationContext synchronizationContext = null;

        #endregion

        #region Instance Constructors (Public)

        public BaseViewModel() : this(SynchronizationContext.Current)
        {

        }

        public BaseViewModel(SynchronizationContext synchronizationContext)
        {
            this.synchronizationContext = synchronizationContext;
        }

        #endregion

        #region Instance Properties (Public)

        public virtual bool CanCopyPasteConfigurations
        {
            get
            {
                return false;
            }
        }

        #endregion

        #region Instance Methods (Protected)

        protected void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                if (this.synchronizationContext != null)
                {
                    this.synchronizationContext.Send((_) =>
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                    }, null);
                }
                else
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }

        #endregion

    }
}
